<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="100px">
      <el-form-item label="发票编号" prop="invoiceCode">
        <el-input v-model="queryParams.invoiceCode" placeholder="请输入发票编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="开票时间" prop="invoiceDate">
        <el-date-picker clearable v-model="queryParams.invoiceDate"
                        type="date" value-format="yyyy-MM-dd" placeholder="请选择开票时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['develop:FinanceBuyInvoice:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['develop:FinanceBuyInvoice:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" width="50"/>
      <el-table-column label="编号" align="center" prop="nos"/>
      <el-table-column label="发票编号" align="center" prop="invoiceCode"/>
      <el-table-column label="金额" align="center" prop="totalAmount"/>
      <el-table-column label="开票时间" align="center" prop="invoiceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="supplierName"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.buy_invoice_status" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column align="center" label="发票" prop="invoiceFile" show-overflow-tooltip>
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.invoiceFile"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)" v-hasPermi="['develop:FinanceBuyInvoice:query']">详情
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.status === '0' || scope.row.status==='2'"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:FinanceBuyInvoice:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleSubmit(scope.row)"
                         v-if="scope.row.status === '0'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-upload2"
                       v-hasPermi="['develop:FinanceBuyInvoice:submit']">提交
            </el-button>
          </el-popconfirm>
          <el-button size="mini" type="text" icon="el-icon-circle-check" v-if="scope.row.status === '1'"
                     @click="handleAssess(scope.row)" v-hasPermi="['develop:FinanceBuyInvoice:submit']">审核
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)"
                         v-if="scope.row.status === '0' || scope.row.status==='2'" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:FinanceBuyInvoice:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <financeBuyInvoice-form ref="form" @ok="getList"></financeBuyInvoice-form>
    <finance-buy-invoice-assess-form ref="invoiceAssessForm" @ok="getList"></finance-buy-invoice-assess-form>
    <finance-buy-invoice-detail ref="invoiceDetail"></finance-buy-invoice-detail>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import FinanceBuyInvoiceForm from '@/views/develop/finance/financeBuyInvoice/module/FinanceBuyInvoiceForm'
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
import FinanceBuyInvoiceAssessForm
  from "@/views/develop/finance/financeBuyInvoice/module/FinanceBuyInvoiceAssessForm.vue";
import FinanceBuyInvoiceDetail from "@/views/develop/finance/financeBuyInvoice/module/FinanceBuyInvoiceDetail.vue";

export default {
  name: "FinanceBuyInvoiceList",
  dicts:["buy_invoice_status"],
  components: {
    FinanceBuyInvoiceDetail,
    FinanceBuyInvoiceAssessForm,
    FileUploadShow,
    FinanceBuyInvoiceForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购发票表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invoiceCode: null,
        invoiceFile: null,
        money: null,
        invoiceDate: null,
        supplierId: null,
        supplierName: null,
        orderByColumn: 'a.nos',
        isAsc: 'desc',
      },
      url: {
        list: '/financeBuyInvoice/financeBuyInvoice/list',
        remove: '/financeBuyInvoice/financeBuyInvoice/',
        export: '/financeBuyInvoice/financeBuyInvoice/export',
        submit: '/financeBuyInvoice/financeBuyInvoice/submit/',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询采购发票列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    handleSubmit(row) {
      const ids = row.id || this.ids;
      getAction(this.url.submit+ids,{}).then(response => {
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => {
      });
    },

    handleAssess(row) {
      this.$refs.invoiceAssessForm.handleUpdate(row);
    },

    handleDetail(row){
      this.$refs.invoiceDetail.handleShow(row);
    },

  /** 导出按钮操作 */
  handleExport() {
    this.download(this.url.export, {
      ...this.queryParams
    }, `financeBuyInvoice_${new Date().getTime()}.xlsx`)
  },
}
}
;
</script>
